﻿/******************************* 
** 作者： 斯泰尔 
** 时间： 2021-09-08 10:39:16 
** 版本: V1.0.0 
** CLR: 4.0.30319.42000 
** GUID: d8542eab-b19c-459d-989a-129e4e30dbf7 
** 描述： 尚未编写描述 
*******************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FDA_Code
{
    class AppCode
    {
        private string xmlhead = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n"
                    + "<Document>\r\n"
                    + "	<Events>\r\n"
                    + "		<Event Name=\"SalesWareHouseOut\" PreAction=\"ProductInfoWrap\" MainAction=\"WareHouseOut\" AfterAction=\"CodeActive\">\r\n"
                    + "			<ActionMaping>\r\n"
                    + "				<Action Name=\"WareHouseOut\">\r\n"
                    + "					<ActionData>CorpOrderID</ActionData>\r\n"
                    + "					<ActionData>Actor</ActionData>\r\n"
                    + "					<ActionData>ActDate</ActionData>\r\n"
                    + "					<ActionData>Code</ActionData>\r\n"
                    + "					<ActionData>ToCorpID</ActionData>\r\n"
                    + "				</Action>\r\n"
                    + "			</ActionMaping>\r\n"
                    + "			<DataMaping>\r\n"
                    + "				<MetaData Name=\"CorpOrderID\" Type=\"String\"/>\r\n"
                    + "				<MetaData Name=\"Actor\" Type=\"String\"/>\r\n"
                    + "				<MetaData Name=\"ActDate\" Type=\"String\"/>\r\n"
                    + "				<MetaData Name=\"Code\" Type=\"String\"/>\r\n"
                    + "				<MetaData Name=\"ToCorpID\" Type=\"String\"/>\r\n"
                    + "			</DataMaping>\r\n"
                    + "			<DataDesc>\r\n"
                    + "				<MetaDataDesc IsParent=\"true\" MetaName=\"Actor\">\r\n"
                    + "					<MetaDataDesc IsParent=\"false\" MetaName=\"Code\"/>\r\n"
                    + "				</MetaDataDesc>\r\n"
                    + "				<MetaDataDesc IsParent=\"true\" MetaName=\"CorpOrderID\">\r\n"
                    + "					<MetaDataDesc IsParent=\"false\" MetaName=\"Code\"/>\r\n"
                    + "				</MetaDataDesc>\r\n"
                    + "			</DataDesc>\r\n"
                    + "			<DataField>\r\n";

        private string xmldata = "				<Data Code=\"{0}\" CorpOrderID=\"{1}\" Actor=\"{2}\" ActDate=\"{3}\" ToCorpID=\"{4}\" />";

        private string xmlfoot = "			</DataField>\r\n"
                         + "		</Event>\r\n"
                         + "	</Events>\r\n"
                         + "</Document>\r\n";


        public void DtHz(DataTable dt)
        {
            DataTable NewDt = new DataTable();
            NewDt.Columns.Add("Code", typeof(string));
            NewDt.Columns.Add("CorpOrderID", typeof(string));
            NewDt.Columns.Add("Actor", typeof(string));
            NewDt.Columns.Add("ActDate", typeof(string));
            NewDt.Columns.Add("ToCorpID", typeof(string));
            NewDt.Columns.Add("ToCorpName", typeof(string));
                    
            foreach (DataRow dr in dt.Rows)
            {
                string[] codes = dr["fda_code"].ToString().Split('#');
                string CorpOrderID = dr["erp_id"].ToString();
                string Actor = dr["code"].ToString();
                string ActDate = dr["created_at"].ToString();
                string ToCorpID = dr["partner_code"].ToString();
                string ToCorpName = dr["partner_name"].ToString();
                foreach (string code in codes)
                {
                    NewDt.Rows.Add(new object[] { code, CorpOrderID, Actor, ActDate, ToCorpID, ToCorpName });
                }
            }
            DataTable Ndt = NewDt.DefaultView.ToTable(true, "CorpOrderID", "ToCorpName").Copy();
            foreach (DataRow dr in Ndt.Rows)
            {
                string CorpOrderID = dr["CorpOrderID"].ToString();
                string ToCorpName = dr["ToCorpName"].ToString();
                DataRow[] ndr = NewDt.Select("CorpOrderID='" + CorpOrderID + "'");
                XMLDt(CorpOrderID + "_" + ToCorpName, ndr);
            }
        }

        public void XMLDt(string filename, DataRow[] drs)
        {
             string resxml="";
             resxml += xmlhead;
             foreach (DataRow dr in drs)
            {
                resxml += string.Format("				<Data Code=\"{0}\" CorpOrderID=\"{1}\" Actor=\"{2}\" ActDate=\"{3}\" ToCorpID=\"{4}\" />\r\n", dr.ItemArray);               
            }
            resxml += xmlfoot;
            SaveFile(filename, resxml);
        }

        public void SaveFile(string filename, string xmltext)
        {

            string text = AppDomain.CurrentDomain.BaseDirectory;
            text = Path.Combine(text, "哈药追溯码");
            if (!Directory.Exists(text))
            {
                Directory.CreateDirectory(text);
            }
            text = Path.Combine(text, filename+"_"+DateTime.Now.ToString("yyyyMMdd") + ".xml");
            if (!File.Exists(text))
            {
                File.CreateText(text).Close();
            }
            StreamWriter streamWriter = new StreamWriter(text, true, Encoding.UTF8);
            streamWriter.WriteLine(xmltext);
            streamWriter.Flush();
            streamWriter.Close();

        }
    }
}
